package com.sskj.level.util;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.level.R;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.level.bean.BuySellEnum;

import org.w3c.dom.Text;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class TipLevelUtil {

    private static int paper;
    private static String minZYPrice;
    private static String maxZYPrice;
    private static String minZSPrice;
    private static String maxZSPrice;
    private static boolean isBuyClass;
    private static boolean isBuy;
    private static TextView tvType;
    private static TextView tvPrice;
    private static TextView tvWinPrice;
    private static TextView tvLowPrice;
    private static Button btSure;
    private static EditText etStopLoseDot;
    private static EditText etStopWinDot;
    private static MaterialDialog dialog;
    private static OnTipResultSure onTipSure;
    private static String winPrice;
    private static String lossPrice;

    public static MaterialDialog getPingcangTip(Context context, boolean isBuy, String price, String maxPaper, OnTipPwdSure onTipPwdSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_pingcang, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        paper = Integer.valueOf(maxPaper);
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvType = customView.findViewById(R.id.tvType);
        TextView tvPrice = customView.findViewById(R.id.tvPrice);
        EditText etNum = customView.findViewById(R.id.etNum);
        TextView tvIncrease = customView.findViewById(R.id.tvIncrease);
        TextView tvDecrease = customView.findViewById(R.id.tvDecrease);
        tvType.setText(isBuy ? App.INSTANCE.getString(R.string.levelJavaTipLevelUtil1) : App.INSTANCE.getString(R.string.levelJavaTipLevelUtil2));
        tvType.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? R.color.level_03c : R.color.level_ff5));
        tvPrice.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? R.color.level_03c : R.color.level_ff5));
        tvPrice.setText(price);
        etNum.setText(maxPaper);
        ClickUtil.click(10, tvDecrease, () -> {
            if (paper <= 0) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.levelJavaTipLevelUtil3));
                return;
            }
            paper--;
            paper = Math.max(0, paper);
            if (paper == 0) {
                etNum.setText("");
                return;
            }
            etNum.setText(paper + "");
        });
        ClickUtil.click(10, tvIncrease, () -> {
            if (paper >= Integer.valueOf(maxPaper)) {
                ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.levelJavaTipLevelUtil5), maxPaper));
                return;
            }
            paper++;
            etNum.setText(paper + "");
        });
        RxTextView.afterTextChangeEvents(etNum)
                .subscribe(textViewAfterTextChangeEvent -> {
                    String text = textViewAfterTextChangeEvent.editable().toString();
                    if (TextUtils.isEmpty(text)) {
                        paper = 0;
                        return;
                    }
                    paper = Integer.valueOf(text);
                });
        Button btCancel = customView.findViewById(R.id.btCancel);
        Button btSure = customView.findViewById(R.id.btSure);
        ClickUtil.click(btCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(btSure, () -> {
            if (paper == 0) {
                ToastUtil.showShort("最少平仓1张");
                return;
            }
            if (paper > Integer.valueOf(maxPaper)) {
                ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.levelJavaTipLevelUtil5), maxPaper));
                return;
            }
            dialog.dismiss();
            onTipPwdSure.onSure(paper + "");
        });

        return dialog;
    }

    public static void showTradeInfo(Context context, String direction, String type,
                                     String price, String num, String winPrice, String lowPrice,
                                     String level, String levels, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_trade_info_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        ((TextView) customeView.findViewById(R.id.tvDirection)).setTextColor(ContextCompat.getColor(App.INSTANCE, App.INSTANCE.getString(R.string.levelJavaTipLevelUtil10).equals(direction) ? R.color.level_05C089 : R.color.level_FF3F3E));
        ((TextView) customeView.findViewById(R.id.tvDirection)).setText(direction);
        ((TextView) customeView.findViewById(R.id.tvType)).setText(type);
        ((TextView) customeView.findViewById(R.id.tvPrice)).setText(price);
        ((TextView) customeView.findViewById(R.id.tvWinPrice)).setText(winPrice);
        ((TextView) customeView.findViewById(R.id.tvLowPrice)).setText(lowPrice);
        ((TextView) customeView.findViewById(R.id.tvLevel)).setText(level);
        ((TextView) customeView.findViewById(R.id.tvNum)).setText(num);
        customeView.findViewById(R.id.llLevel).setVisibility(levels.split(",").length > 1 ? View.VISIBLE : View.GONE);
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
    }

    public static void showConfirmOrder(Context context, String direction, String type,
                                        String price, String num, String bzj, String ggbs, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_confirm_order, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customeView = dialog.getCustomView();

        TextView tvDirection = (TextView) customeView.findViewById(R.id.tvDirection);
        tvDirection.setText(direction);
        tvDirection.setTextColor(context.getResources().getColor(context.getString(R.string.levelCurrentHolderFragment0).equals(direction) ?R.color.level_05C089 : R.color.level_FF3F3E));
        ((TextView) customeView.findViewById(R.id.tvType)).setText(type);
        ((TextView) customeView.findViewById(R.id.tvPrice)).setText(price);
        ((TextView) customeView.findViewById(R.id.tvbzj)).setText(bzj);
        ((TextView) customeView.findViewById(R.id.tvggbs)).setText(ggbs);
        ((TextView) customeView.findViewById(R.id.tvNum)).setText(num);
        ClickUtil.click(customeView.findViewById(R.id.tvCancel), () -> {
            dialog.dismiss();
        });
        ClickUtil.click(customeView.findViewById(R.id.tvSure), () -> {
            dialog.dismiss();
            onTipSure.onSure();
        });
    }

    public static MaterialDialog showTradeSet(Context context,
                                              String type,
                                              String newPrice,
                                              String minZY,
                                              String maxZY,
                                              String minZS,
                                              String maxZS,
                                              OnTipResultSure onTipSure) {

        dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_trade_set_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();

        minZSPrice = minZS;
        maxZSPrice = maxZS;
        minZYPrice = minZY;
        maxZYPrice = maxZY;
        TipLevelUtil.onTipSure = onTipSure;
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        tvType = customView.findViewById(R.id.tvType);
        tvPrice = customView.findViewById(R.id.tvPrice);
        tvWinPrice = customView.findViewById(R.id.tvWinPrice);
        tvLowPrice = customView.findViewById(R.id.tvLowPrice);
        Button btCancel = customView.findViewById(R.id.btCancel);
        btSure = customView.findViewById(R.id.btSure);
        etStopLoseDot = customView.findViewById(R.id.etStopLoseDot);
        etStopWinDot = customView.findViewById(R.id.etStopWinDot);

        isBuy = type.equals(BuySellEnum.BUY.getType());
        updatePrice(newPrice);

        ((TextView) customView.findViewById(R.id.etStopLoseDot)).setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        ((TextView) customView.findViewById(R.id.etStopWinDot)).setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});


        ClickUtil.click(btCancel, () -> {
            dialog.dismiss();
        });

        return dialog;
    }

    private static void updatePrice(String newPrice) {
        tvType.setText(isBuy ? BuySellEnum.BUY.getTitle() : BuySellEnum.SELL.getTitle());
        tvType.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()));
        tvPrice.setText(newPrice);

//        tvPrice.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()));
        winPrice = isBuy ?
                new BigDecimal(newPrice).add(new BigDecimal(minZYPrice)).toPlainString() :
                new BigDecimal(newPrice).subtract(new BigDecimal(minZYPrice)).toPlainString();
        if (Double.valueOf(winPrice) < 0) {
            winPrice = "0";
        }
        lossPrice = isBuy ?
                new BigDecimal(newPrice).subtract(new BigDecimal(minZSPrice)).toPlainString() :
                new BigDecimal(newPrice).add(new BigDecimal(minZSPrice)).toPlainString();
        if (Double.valueOf(lossPrice) < 0) {
            lossPrice = "0";
        }
//        String winMaxPrice = isBuy ?
//                new BigDecimal(newPrice).add(new BigDecimal(maxZYPrice)).toPlainString() :
//                new BigDecimal(newPrice).subtract(new BigDecimal(minZYPrice)).toPlainString();
//        String lossMinPrice = isBuy ?
//                new BigDecimal(newPrice).subtract(new BigDecimal(maxZSPrice)).toPlainString() :
//                new BigDecimal(newPrice).add(new BigDecimal(minZSPrice)).toPlainString();
        if (isBuy) {
            tvWinPrice.setText("≥" + winPrice);
            tvLowPrice.setText("≤" + lossPrice);
        } else {
            tvWinPrice.setText("≤" + winPrice);
            tvLowPrice.setText("≥" + lossPrice);
        }
        ClickUtil.click(btSure, () -> {
//            if (TextUtils.isEmpty(etStopLoseDot.getText()) && TextUtils.isEmpty(etStopWinDot.getText())) {
//                ToastUtil.showShort(App.INSTANCE.getString(R.string.levelTipUtil1));
//                return;
//            }
            if (!TextUtils.isEmpty(etStopWinDot.getText()) && Double.valueOf(etStopWinDot.getText().toString()) != 0) {
//                if (Double.valueOf(etStopWinDot.getText().toString()) > Double.valueOf(winMaxPrice)) {
//                    ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_100), winMaxPrice));
//                    return;
//                }
                if (isBuy) {
                    if (Double.valueOf(etStopWinDot.getText().toString()) < Double.valueOf(winPrice)) {
                        ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_101), winPrice));
                        return;
                    }
                } else {
                    if (Double.valueOf(etStopWinDot.getText().toString()) > Double.valueOf(winPrice)) {
                        ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_100), winPrice));
                        return;
                    }
                }
            }
            if (!TextUtils.isEmpty(etStopLoseDot.getText()) && Double.valueOf(etStopLoseDot.getText().toString()) != 0) {
                if (isBuy) {
                    if (Double.valueOf(etStopLoseDot.getText().toString()) > Double.valueOf(lossPrice)) {
                        ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_102), lossPrice));
                        return;
                    }
                } else {
                    if (Double.valueOf(etStopLoseDot.getText().toString()) < Double.valueOf(lossPrice)) {
                        ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_103), lossPrice));
                        return;
                    }
                }
//                if (Double.valueOf(etStopLoseDot.getText().toString()) < Double.valueOf(lossMinPrice)) {
//                    ToastUtil.showShort(String.format(App.INSTANCE.getString(R.string.level_tiplevel_103), lossMinPrice));
//                    return;
//                }
            }

            dialog.dismiss();
            ArrayList<String> result = new ArrayList<>();
            result.add(etStopWinDot.getText().toString());
            result.add(etStopLoseDot.getText().toString());
            onTipSure.onSure(result);
        });
    }

    public static void changeTradeSet(MaterialDialog dialog, String newPrice) {
        updatePrice(newPrice);
    }

    public static MaterialDialog getTip(Context context, String content, OnTipSure onTipSure) {
        return getTip(context, content, App.INSTANCE.getString(R.string.level_dialog_trade_set_tip130), onTipSure);
    }

    public static MaterialDialog getTip(Context context, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(R.string.level_tip_title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(R.string.level_cancel)
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public static MaterialDialog showTradeCancel(Context context, String type,
                                                 String num, String price, OnTipSure onTipSure) {
        MaterialDialog dialog = new MaterialDialog.Builder(context)
                .customView(R.layout.level_dialog_trade_cancel_tip, false)
                .autoDismiss(false)
                .cancelable(false)
                .show();
        View customView = dialog.getCustomView();
        ((ViewGroup) (customView.getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        ((ViewGroup) (customView.getParent().getParent().getParent().getParent().getParent())).setBackgroundColor(Color.TRANSPARENT);
        TextView tvType = customView.findViewById(R.id.tvType);
        TextView tvPrice = customView.findViewById(R.id.tvPrice);
        TextView tvNum = customView.findViewById(R.id.tvNum);
        Button btCancel = customView.findViewById(R.id.btCancel);
        Button btSure = customView.findViewById(R.id.btSure);
        boolean isBuy = type.equals(BuySellEnum.BUY.getType());
        tvType.setText(isBuy ? BuySellEnum.BUY.getTitle() : BuySellEnum.SELL.getTitle());
        tvType.setTextColor(ContextCompat.getColor(App.INSTANCE, isBuy ? BuySellEnum.BUY.getColorRes() : BuySellEnum.SELL.getColorRes()));
        tvNum.setText(num);
        tvPrice.setText(price);


        ClickUtil.click(btCancel, () -> {
            dialog.dismiss();
        });
        ClickUtil.click(btSure, () -> {

            onTipSure.onSure();
        });
        return dialog;
    }

    public static MaterialDialog getTip(Context context, String title, String content, String sureMsg, OnTipSure onTipSure) {
        return new MaterialDialog.Builder(context)
                .title(title)
                .content(content)
                .positiveText(sureMsg)
                .negativeText(R.string.level_cancel)
                .onAny((dialog, which) -> {
                    if (which == DialogAction.POSITIVE) {
                        dialog.dismiss();
                        onTipSure.onSure();
                    } else if (which == DialogAction.NEGATIVE) {
                        dialog.dismiss();
                    }
                }).build();
    }

    public interface OnTipSure {
        void onSure();
    }

    public interface OnTipPwdSure {
        void onSure(String pwd);
    }

    public interface OnTipResultSure {
        void onSure(List<String> list);
    }
}
